package com.sz.common.core.service.sequence;

import com.sz.common.base.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Package: com.sz.common.core.service.sequence <br>
 * Function: TODO: ADD FUNCTION <br>
 * Author: Charles. <br>
 * Date: 2017-04-08 11:16:00.
 */
@Service
public class SequenceService {
    @Autowired
    Dao dao;

    public int nextValue() {
        return nextValue("global");
    }

    /**
     * @param sequenceName
     * @return
     */
    public int nextValue(String sequenceName) {
        if (null == sequenceName || sequenceName.length() == 0) {
            throw new IllegalArgumentException("sequenceName cannot be empty");
        }
        Map<String, String> map = new HashMap<>();
        map.put("sequence", sequenceName);
        map.put("random", UUID.randomUUID().toString());
        return (int)dao.findForObject(getMapperNamespace() + ".getSequence", map);
    }

    private String getMapperNamespace() {
        return "com.sz.common.core.system.mapper.SysSequenceMapper";
    }
}
